package com.tbit.main.service.impl;

import com.tbit.main.pojo.*;
import com.tbit.main.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author LMY
 * @create 2022-03-30 15:46
 */
@Service("accountCheckingService")
public class AccountCheckingServiceImpl implements AccountCheckingService {

    @Autowired
    private BrandDataDayStaService brandDataDayStaService;
    @Autowired
    private AccountDataDayStaService accountDataDayStaService;
    @Autowired
    private BrandSerialDayStaService brandSerialDayStaService;
    @Autowired
    private UserService userService;
    @Autowired
    private MonthDateTypeService monthDateTypeService;
    @Autowired
    private AccountService accountService;

    /**
     * 资金流水=骑行卡订单+会员卡订单+免押卡订单+优惠券订单+用户充值金额+押金支付金额-骑行卡订单退款-会员卡订单退款-免押卡订单退款-优惠券订单退款-充值退款金额-押金退款金额
     * 运营收益=骑行卡订单+会员卡订单+免押卡订单+优惠券订单+骑行订单支付金额-骑行卡订单退款-会员卡订单退款-免押卡订单退款-优惠券订单退款-骑行订单退款金额
     *
     * 用户余额变更（差值）=用户充值金额+手动充值余额+骑行订单退款金额-充值退款金额-手动扣款余额-骑行订单支付金额
     * 押金变更（差值）=押金支付金额-押金退款金额
     *
     * 资金流水=运营收益+用户余额+用户押金
     * */

    /**
     * 品牌总数据
     *
     * @param brandId
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public BrandDataDaySta totalDataBrandId(Integer brandId, String startTime, String endTime) {

        /**获取品牌数据总数*/
        BrandDataDaySta brandDataDaySta = brandDataDayStaService.getByBrandIdAndTimeCount(brandId, startTime, endTime);

        /**拼接数据*/
        if (brandDataDaySta != null) {
            brandDataDaySta.setIncome(brandDataDaySta.getRideCard().add(brandDataDaySta.getVipCard()).add(brandDataDaySta.getDepositCard()).add(
                    brandDataDaySta.getCouponOrder()).add(brandDataDaySta.getOrderPayMoney()).subtract(brandDataDaySta.getRideCardRefund()).subtract(
                    brandDataDaySta.getVipCardRefund()).subtract(brandDataDaySta.getDepositCardRefund()).subtract(brandDataDaySta.getCouponOrderRefund()).subtract(
                    brandDataDaySta.getRideOrderRefund()));
            brandDataDaySta.setSpringWater(brandDataDaySta.getRideCard().add(brandDataDaySta.getVipCard()).add(brandDataDaySta.getDepositCard()).add(
                    brandDataDaySta.getCouponOrder()).add(brandDataDaySta.getRechargeMoney()).add(brandDataDaySta.getUserCommitPledge()).subtract(
                    brandDataDaySta.getRideCardRefund()).subtract(brandDataDaySta.getVipCardRefund()).subtract(
                    brandDataDaySta.getDepositCardRefund()).subtract(brandDataDaySta.getCouponOrderRefund()).subtract(
                    brandDataDaySta.getRechargeRefund()).subtract(brandDataDaySta.getUserRefundPledge()));
            brandDataDaySta.setBalanceChanges(brandDataDaySta.getRechargeMoney().add(brandDataDaySta.getManualRechargeMoney()).add(
                    brandDataDaySta.getRideOrderRefund()).subtract(brandDataDaySta.getRechargeRefund()).subtract(brandDataDaySta.getManualDeductMoney()).subtract(
                    brandDataDaySta.getOrderPayMoney()));
            brandDataDaySta.setDepositChange(brandDataDaySta.getUserCommitPledge().subtract(brandDataDaySta.getUserRefundPledge()));
        }

        if (brandDataDaySta == null) {
            brandDataDaySta = new BrandDataDaySta();
        }
        brandDataDaySta.setRemaining(userService.getUserMoneyCount(brandId));
        KVEntity kvEntity = userService.getUserDepositCount(brandId);
        brandDataDaySta.setDeposit(kvEntity.getV());
        return brandDataDaySta;
    }

    /**
     * 区域总数据
     *
     * @param accountIds
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public List<BrandDataDaySta> totalDataAccountId(List<Integer> accountIds, String startTime, String endTime) {
        /**获取区域数据总数*/
        List<BrandDataDaySta> accountDataDayStaList = accountDataDayStaService.getByAccountIdAndTimeCount(accountIds, startTime, endTime);
        /**区域数据*/
        List<Account> accountList = accountService.getAccountIds(accountIds);
        Map<Integer, String> mapAccount = new HashMap<>();
        for (Account account : accountList) {
            mapAccount.put(account.getAccountId(), account.getName());
        }

        /**拼接数据*/
        Map<Integer, BrandDataDaySta> map = new HashMap<>();
        if (accountDataDayStaList != null && !accountDataDayStaList.isEmpty()) {
            for (BrandDataDaySta accountDataDaySta : accountDataDayStaList) {
                accountDataDaySta.setIncome(accountDataDaySta.getRideCard().add(accountDataDaySta.getVipCard()).add(accountDataDaySta.getDepositCard()).add(
                        accountDataDaySta.getCouponOrder()).add(accountDataDaySta.getOrderPayMoney()).subtract(accountDataDaySta.getRideCardRefund()).subtract(
                        accountDataDaySta.getVipCardRefund()).subtract(accountDataDaySta.getDepositCardRefund()).subtract(accountDataDaySta.getCouponOrderRefund()).subtract(
                        accountDataDaySta.getRideOrderRefund()));
                accountDataDaySta.setAccountIdName(mapAccount.get(accountDataDaySta.getAccountId()));
                map.put(accountDataDaySta.getAccountId(), accountDataDaySta);
            }
        }

        List<BrandDataDaySta> brandDataDayStas = new LinkedList<>();
        for (Integer accountId : accountIds) {
            if (map.containsKey(accountId)) {
                brandDataDayStas.add(map.get(accountId));
            } else {
                BrandDataDaySta brandDataDaySta = new BrandDataDaySta();
                brandDataDaySta.setAccountId(accountId);
                brandDataDaySta.setAccountIdName(mapAccount.get(accountId));
                brandDataDayStas.add(brandDataDaySta);
            }
        }

        return brandDataDayStas;
    }

    @Override
    public List<BrandDataDaySta> brandIdDay(Integer brandId, String startTime, String endTime) {

        /**获取品牌数据天*/
        List<BrandDataDaySta> brandDataDayStaList = brandDataDayStaService.getByBrandIdAndTime(brandId, startTime, endTime);

        /**获取品牌资金数据天*/
        List<BrandSerialDaySta> brandSerialDayStaList = brandSerialDayStaService.getByBrandIdAndTime(brandId, startTime, endTime);
        Map<String, List<BrandSerialDaySta>> brandSerialDayStaMap = new HashMap<>();
        for (BrandSerialDaySta brandSerialDaySta : brandSerialDayStaList) {
            if (brandSerialDayStaMap.containsKey(brandSerialDaySta.getOrderDate() + brandSerialDaySta.getBrandId())) {
                brandSerialDayStaMap.get(brandSerialDaySta.getOrderDate() + brandSerialDaySta.getBrandId()).add(brandSerialDaySta);
            } else {
                List<BrandSerialDaySta> brandSerialDayStaS = new LinkedList<>();
                brandSerialDayStaS.add(brandSerialDaySta);
                brandSerialDayStaMap.put(brandSerialDaySta.getOrderDate() + brandSerialDaySta.getBrandId(), brandSerialDayStaS);
            }
        }

        /**拼接数据*/
        for (BrandDataDaySta brandDataDaySta : brandDataDayStaList) {
            brandDataDaySta.setIncome(brandDataDaySta.getRideCard().add(brandDataDaySta.getVipCard()).add(brandDataDaySta.getDepositCard()).add(
                    brandDataDaySta.getCouponOrder()).add(brandDataDaySta.getOrderPayMoney()).subtract(brandDataDaySta.getRideCardRefund()).subtract(
                    brandDataDaySta.getVipCardRefund()).subtract(brandDataDaySta.getDepositCardRefund()).subtract(brandDataDaySta.getCouponOrderRefund()).subtract(
                    brandDataDaySta.getRideOrderRefund()));
            brandDataDaySta.setSpringWater(brandDataDaySta.getRideCard().add(brandDataDaySta.getVipCard()).add(brandDataDaySta.getDepositCard()).add(
                    brandDataDaySta.getCouponOrder()).add(brandDataDaySta.getRechargeMoney()).add(brandDataDaySta.getUserCommitPledge()).subtract(
                    brandDataDaySta.getRideCardRefund()).subtract(brandDataDaySta.getVipCardRefund()).subtract(
                    brandDataDaySta.getDepositCardRefund()).subtract(brandDataDaySta.getCouponOrderRefund()).subtract(
                    brandDataDaySta.getRechargeRefund()).subtract(brandDataDaySta.getUserRefundPledge()));
            brandDataDaySta.setBalanceChanges(brandDataDaySta.getRechargeMoney().add(brandDataDaySta.getManualRechargeMoney()).add(
                    brandDataDaySta.getRideOrderRefund()).subtract(brandDataDaySta.getRechargeRefund()).subtract(brandDataDaySta.getManualDeductMoney()).subtract(
                    brandDataDaySta.getOrderPayMoney()));
            brandDataDaySta.setDepositChange(brandDataDaySta.getUserCommitPledge().subtract(brandDataDaySta.getUserRefundPledge()));
            if (brandSerialDayStaMap.containsKey(brandDataDaySta.getOrderDate() + brandDataDaySta.getBrandId())) {
                for (BrandSerialDaySta brandSerialDaySta : brandSerialDayStaMap.get(brandDataDaySta.getOrderDate() + brandDataDaySta.getBrandId())) {
                    if (brandSerialDaySta.getType() == 0) {
                        brandDataDaySta.setWx(brandSerialDaySta.getOrderMoney());
                    } else {
                        brandDataDaySta.setZfb(brandSerialDaySta.getOrderMoney());
                    }
                }
            } else {
                brandDataDaySta.setWx(BigDecimal.valueOf(0.00));
                brandDataDaySta.setZfb(BigDecimal.valueOf(0.00));
            }
        }
        return brandDataDayStaList;
    }

    @Override
    public List<BrandDataDaySta> brandIdMonth(Integer brandId, String startTime, String endTime) {

        /**获取品牌数据月统计*/
        List<BrandDataDaySta> brandDataDayStaList = brandDataDayStaService.getByBrandIdAndMonthTime(brandId, startTime, endTime);

        /**拼接数据*/
        for (BrandDataDaySta brandDataDaySta : brandDataDayStaList) {
            brandDataDaySta.setIncome(brandDataDaySta.getRideCard().add(brandDataDaySta.getVipCard()).add(brandDataDaySta.getDepositCard()).add(
                    brandDataDaySta.getCouponOrder()).add(brandDataDaySta.getOrderPayMoney()).subtract(brandDataDaySta.getRideCardRefund()).subtract(
                    brandDataDaySta.getVipCardRefund()).subtract(brandDataDaySta.getDepositCardRefund()).subtract(brandDataDaySta.getCouponOrderRefund()).subtract(
                    brandDataDaySta.getRideOrderRefund()));
            brandDataDaySta.setSpringWater(brandDataDaySta.getRideCard().add(brandDataDaySta.getVipCard()).add(brandDataDaySta.getDepositCard()).add(
                    brandDataDaySta.getCouponOrder()).add(brandDataDaySta.getRechargeMoney()).add(brandDataDaySta.getUserCommitPledge()).subtract(
                    brandDataDaySta.getRideCardRefund()).subtract(brandDataDaySta.getVipCardRefund()).subtract(
                    brandDataDaySta.getDepositCardRefund()).subtract(brandDataDaySta.getCouponOrderRefund()).subtract(
                    brandDataDaySta.getRechargeRefund()).subtract(brandDataDaySta.getUserRefundPledge()));
            brandDataDaySta.setBalanceChanges(brandDataDaySta.getRechargeMoney().add(brandDataDaySta.getManualRechargeMoney()).add(
                    brandDataDaySta.getRideOrderRefund()).subtract(brandDataDaySta.getRechargeRefund()).subtract(brandDataDaySta.getManualDeductMoney()).subtract(
                    brandDataDaySta.getOrderPayMoney()));
            brandDataDaySta.setDepositChange(brandDataDaySta.getUserCommitPledge().subtract(brandDataDaySta.getUserRefundPledge()));
        }
        return brandDataDayStaList;
    }

    @Override
    public Map<String, List<AccountDataDayStaReturn>> accountIdDay(List<Integer> accountIds, String startTime, String endTime) {

        /**区域数据*/
        List<Account> accountList = accountService.getAccountIds(accountIds);
        Map<Integer, String> mapAccount = new HashMap<>();
        for (Account account : accountList) {
            mapAccount.put(account.getAccountId(), account.getName());
        }

        /**获取区域数据天*/
        List<AccountDataDaySta> accountDataDayStaList = accountDataDayStaService.getByAccountIdAndTime(accountIds, startTime, endTime);

        Map<String, List<AccountDataDayStaReturn>> map = new HashMap<>();
        List<AccountDataDayStaReturn> accountDataDayStaReturnList = new LinkedList<>();
        for (Integer accountId : accountIds) {
            AccountDataDayStaReturn accountDataDayStaReturn = new AccountDataDayStaReturn();
            accountDataDayStaReturn.setAccountId(accountId);
            accountDataDayStaReturn.setAccountName(mapAccount.get(accountId));
            List<AccountDataDaySta> accountDataDayStaS = new LinkedList<>();
            /**拼接数据*/
            for (AccountDataDaySta accountDataDaySta : accountDataDayStaList) {
                if (accountDataDaySta.getAccountId().equals(accountId)) {
                    accountDataDaySta.setIncome(accountDataDaySta.getRideCard().add(accountDataDaySta.getVipCard()).add(accountDataDaySta.getDepositCard()).add(
                            accountDataDaySta.getCouponOrder()).add(accountDataDaySta.getOrderPayMoney()).subtract(accountDataDaySta.getRideCardRefund()).subtract(
                            accountDataDaySta.getVipCardRefund()).subtract(accountDataDaySta.getDepositCardRefund()).subtract(accountDataDaySta.getCouponOrderRefund()).subtract(
                            accountDataDaySta.getRideOrderRefund()));
                    accountDataDayStaS.add(accountDataDaySta);
                }
            }
            accountDataDayStaReturn.setDataList(accountDataDayStaS);
            accountDataDayStaReturnList.add(accountDataDayStaReturn);
        }
        map.put("list", accountDataDayStaReturnList);
        return map;
    }

    @Override
    public Map<String, List<AccountDataDayStaReturn>> accountIdMonth(List<Integer> accountIds, String startTime, String endTime) {

        /**区域数据*/
        List<Account> accountList = accountService.getAccountIds(accountIds);
        Map<Integer, String> mapAccount = new HashMap<>();
        for (Account account : accountList) {
            mapAccount.put(account.getAccountId(), account.getName());
        }

        /**获取订单时间统计*/
        List<MonthDateType> monthDateTypeList = monthDateTypeService.getByAccountId(accountIds, startTime, endTime);
        /**区域，类型，数据*/
        Map<String, Map<String, List<MonthDateType>>> monthDateTypeMap = new HashMap<>();
        for (MonthDateType monthDateType : monthDateTypeList) {
            if (monthDateTypeMap.containsKey(monthDateType.getAccountId().toString())) {
                Map<String, List<MonthDateType>> map = monthDateTypeMap.get(monthDateType.getAccountId().toString());
                if (map.containsKey(monthDateType.getType().toString())) {
                    map.get(monthDateType.getType().toString()).add(monthDateType);
                } else {
                    List<MonthDateType> monthDateTypeS = new LinkedList<>();
                    monthDateTypeS.add(monthDateType);
                    map.put(monthDateType.getType().toString(), monthDateTypeS);
                    monthDateTypeMap.get(monthDateType.getAccountId().toString()).putAll(map);
                }
            } else {
                Map<String, List<MonthDateType>> map = new HashMap<>();
                List<MonthDateType> monthDateTypeS = new LinkedList<>();
                monthDateTypeS.add(monthDateType);
                map.put(monthDateType.getType().toString(), monthDateTypeS);
                monthDateTypeMap.put(monthDateType.getAccountId().toString(), map);
            }
        }

        Map<String, List<AccountDataDayStaReturn>> map = new HashMap<>();
        map.put("list", new LinkedList<>());

        /**拼接数据*/
        for (Integer accountId : accountIds) {
            AccountDataDayStaReturn accountDataDayStaReturn = new AccountDataDayStaReturn();
            accountDataDayStaReturn.setAccountId(accountId);
            accountDataDayStaReturn.setAccountName(mapAccount.get(accountId));
            accountDataDayStaReturn.setMonthTypeReturns(monthDateTypeMap.get(accountId.toString()));
            map.get("list").add(accountDataDayStaReturn);
        }
        return map;
    }

}
